Pointer to Pointer 顧名思義就是指標的指標~
它可能是一個變數的地址的地址~
我們要在function間~傳遞Pointer的時候~ 會用到Pointer to Pointer~
學習目標: Pointer to Pointer的概念及實務
學習難度: ☆☆★
Pointer函式傳輸parameter沒side effect
#include <iostream>
using namespace std;
int value1 = 5;
void changePtr (int *valuepointer)
{
valuepointer = &value1;
}
int main ()
{
int value2 = 10;
int *value2Ptr = &value2;
changePtr (value2Ptr);//因為這裡還是copy的概念....類似call by value
printf ("%d\n", *value2Ptr); //will print 10
return 0;
}
Pointer Pointer函式傳輸parameter有side effect
#include <iostream>
using namespace std;
int value1 = 0;
void changePtr (int **valuepointer) //value2指標的地址
{
*valuepointer = &value1; //value2指標= value1地址
}
int main ()
{
int value2 = 1; //value2整數
int *value2Ptr = &value2; //value2指標
changePtr (&value2Ptr); //取value2指標的地址丟去函式
printf ("%d\n", *value2Ptr);///will print 5
return 0;
}
參考資料:
http://low-understated.blogspot.com/2009/04/pointer-to-pointer.html